<?php

class FeedbackAdmin extends AdminBase
{
	private $nohf = ['handle', 'dohandle'];
	private $mysql = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
	}

	public function index()
	{
		$feedInfos = $this->mysql->select("select * from feedback_main where reply is null order by ctime desc");
		foreach ($feedInfos as $feedInfo) {
			$ppidArr[] = $feedInfo['ppid'];
		}
		$rs = $this->mysql->select('select * from passport_main where id in (' . implode(',', $ppidArr) . ')');
		foreach ($rs as $value) {
			$userArr[$value['id']] = $value;
		}
		foreach ($feedInfos as $feedInfo) {
			$feedInfo['userInfo'] = $userArr[$feedInfo['ppid']];
			$data[] = $feedInfo;
		}
		$this->_html('adminIndex', $data);
	}

	public function handle()
	{
		$param = $this->param('id:char:empty');
		$id = $param['id'];

		$this->_html('handle', $id);
	}

	public function doHandle()
	{
		$param = $this->param('id:char:empty,reply:char:empty');
		$id = $param['id'];

		$data = [
			'reply' => $param['reply'],
			'reply_ppid' => $_SESSION['ppid'],
			'rtime' => time()
		];

		$where = "id = $id";
		$this->mysql->update('feedback_main', $data, $where);
		n_mod("skin")->save("保存成功",3);
	}

	public function handled()
	{
		$handled = $this->mysql->select('select * from feedback_main where reply is not null order by rtime desc');
		foreach ($handled as $value) {
			$userPpidArr[] = $value['ppid'];
			$replyPpidArr[] = $value['reply_ppid'];
		}
		$rs = $this->mysql->select('select * from passport_main where id in (' . implode(',', $replyPpidArr) . ')');
		foreach ($rs as $value) {
			$replyArr[$value['id']] = $value;
		}
		$rs = $this->mysql->select('select * from passport_main where id in (' . implode(',', $userPpidArr) . ')');
		foreach ($rs as $value) {
			$userArr[$value['id']] = $value;
		}
		foreach ($handled as $value) {
			$value['replyInfo'] = $replyArr[$value['reply_ppid']];
			$value['userInfo'] = $userArr[$value['ppid']];
			$data[] = $value;
		}
		$this->_html('handled', $data);
	}


	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}

}